package com.kdtech.analyse.news;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

public class GndailyNewsAnalyse implements AnalyseNews{
	
	private static final String[] regex={
		
				"http://.*.gndaily.com/.*[0-9]{4}-[0-9]{1,2}/[0-9]{1,2}/content_[0-9]*.htm",
				"http://auto.gndaily.com/contents/[a-zA-Z]*/[0-9]*.html",
				
	};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}
			
			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}

			

			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}		
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();		
				
				String title=null;
				String content=null;
				String author=null;
				Long date=null;
				Document doc=Jsoup.parse(htmltxt);
				title=doc.select("div.bt").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("td.page_bt").text();
					if(StringUtils.isBlank(title)){
						title=doc.select("div#contents h1").text();
					}
				}
				content=HtmlCleaner.getContentHtml(url,doc.select("font[style=font-size:16px; LINE-HEIGHT: 180%]"));
				if(StringUtils.isBlank(content)){
					content =doc.select("td.zj3").text();
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.dis_contact"));
					}
				}
				if(StringUtils.isBlank(content))
					content=HtmlCleaner.getContentHtml(url,doc.select("div#Zoom p"));

				/*
				 * 作者
				 */
				author=JSoupUtils.matchAuthor(doc, "来源：");

				date=DateUtils.matchDate(doc.select("td[width=150]").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("td.zj2").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("p.dis_time").text());
					}
				}
//				if(StringUtils.isBlank(title)||date==null){
//				}
				news.setUrl(url);		
				news.setTitle(title);
				news.setContent(content);
				news.setAuthor(author);
				news.setDate(date);
				return news;
			}
			
			public static void main(String[] args) {
				GndailyNewsAnalyse test=new GndailyNewsAnalyse();
				String url="http://www.gndaily.com/kjgz/2016-11/08/content_1721924.htm";
				url = UrlArgumentTop.FromatUrl(url);
				System.out.println(UrlArgumentTop.FromatUrl(url));
				System.out.println(test.isDetailPage(url));
				UrlMeta meta=CrawlHTML.responseToURL(url);
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
